package com.wangyadong.hobby.thread;

import org.eclipse.jetty.util.thread.ExecutorThreadPool;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class CyclicBarrierTest {

    private static final CyclicBarrier BARRIER = new CyclicBarrier(2, () -> {
        System.out.println(Thread.currentThread().getName() + " 执行屏障");
    });

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {

        long startTime = System.currentTimeMillis();
        Runnable runnable = () -> {
            try {
                System.out.println(Thread.currentThread().getName() + "-----------开始等待-----------------" + BARRIER.getNumberWaiting() + " b.getParties()" + BARRIER.getParties());
                int await = BARRIER.await();
                System.out.println(Thread.currentThread().getName() + "--------------执行--------------" + BARRIER.getNumberWaiting() + "     " + await);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        };
        IntStream.range(0, 1).forEach(i -> {
            new Thread(runnable).start();
        });
//        TimeUnit.SECONDS.sleep(3);
        System.out.println("============================" + BARRIER.getNumberWaiting());
        int await = BARRIER.await();
        System.out.println("before await is " + await);
        BARRIER.reset();
        await = BARRIER.await();
        System.out.println("after await is " + await);
    }
}
